<template>
  <div class="login-container">
    <div class="login-box">
      <div class="login-header">
        <h2>商品防伪网站</h2>
        <p>用户登录</p>
      </div>
      
      <el-form :model="loginForm" :rules="loginRules" ref="loginFormRef" class="login-form">
        <el-form-item prop="username">
          <el-input 
            v-model="loginForm.username" 
            :prefix-icon="User"
            placeholder="请输入用户名">
          </el-input>
        </el-form-item>
        
        <el-form-item prop="password">
          <el-input 
            v-model="loginForm.password" 
            :prefix-icon="Lock"
            type="password" 
            placeholder="请输入密码">
          </el-input>
        </el-form-item>
        
        <el-form-item prop="role">
          <el-select v-model="loginForm.role" placeholder="请选择用户角色" style="width: 100%">
            <el-option label="管理员" value="ADMIN"></el-option>
            <el-option label="生产商" value="FARMER"></el-option>
            <el-option label="经销商" value="DEALER"></el-option>
          </el-select>
        </el-form-item>
        
        <el-form-item>
          <el-button type="primary" @click="submitLogin" style="width: 100%" :loading="loading">登录</el-button>
        </el-form-item>
      </el-form>
      
      <div class="login-actions">
        <router-link to="/register">没有账号？立即注册</router-link>
        <router-link to="/trace" class="guest-link">溯源查询</router-link>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { User, Lock } from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'
import { useRouter } from 'vue-router'
import { useStore } from 'vuex'

const router = useRouter()
const store = useStore()
const loginFormRef = ref(null)

const loginForm = reactive({
  username: '',
  password: '',
  role: 'ADMIN'
})

const loading = ref(false)

const loginRules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' }
  ],
  role: [
    { required: true, message: '请选择角色', trigger: 'change' }
  ]
}

const submitLogin = () => {
  loginFormRef.value.validate(valid => {
    if (!valid) return
    
    // Usar la acción login del store
    loading.value = true
    store.dispatch('login', loginForm)
      .then(() => {
        ElMessage({
          message: '登录成功',
          type: 'success',
          duration: 2000
        })
        
        // 根据角色跳转到对应的首页
        const redirectMap = {
          'ADMIN': '/admin',
          'FARMER': '/farmer',
          'DEALER': '/dealer'
        }
        
        router.push(redirectMap[loginForm.role] || '/')
      })
      .catch(error => {
        ElMessage({
          message: error || '登录失败，请检查账号密码',
          type: 'error',
          duration: 5000
        })
      })
      .finally(() => {
        loading.value = false
      })
  })
}
</script>

<style scoped>
.login-container {
  min-height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: #f0f2f5;
}

.login-box {
  width: 350px;
  padding: 30px;
  background-color: #fff;
  border-radius: 4px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.login-header {
  text-align: center;
  margin-bottom: 30px;
}

.login-header h2 {
  font-size: 22px;
  color: #303133;
  margin-bottom: 10px;
}

.login-header p {
  font-size: 16px;
  color: #606266;
}

.login-form {
  margin-bottom: 20px;
}

.login-actions {
  display: flex;
  justify-content: space-between;
  font-size: 14px;
}

.login-actions a {
  color: #409EFF;
  text-decoration: none;
}

.guest-link {
  color: #909399;
}
</style> 